package com.boc.langxiang.ui.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.boc.langxiang.manager.UserInfoManager;
import com.boc.langxiang.ui.activity.common.RegistAndLoginActivity;
import com.boc.langxiang.ui.dialog.CustomAlertDialog;
import com.boc.langxiang.ui.dialog.WaitingDialog;
import com.boc.langxiang.util.L;
import com.boc.langxiang.util.StringUtil;
import com.boc.langxiang.util.T;


/**
 * CreateTime: 2016-06-14上午10:51
 * Author: wjkjinke00@126.com
 * Description:
 */
public class BaseFragment extends Fragment {

    protected Context mContext;
    private WaitingDialog mWaitingDialog;
    private CustomAlertDialog mCustomAlertDialog;
    private long mExitTime;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mContext = activity;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        L.d("--fragment onPause -> " + getClass().getSimpleName());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        L.d("--fragment, onCreateView -> " + getClass().getSimpleName());
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        L.d("--fragment, onViewCreated -> " + getClass().getSimpleName());
    }

    @Override
    public void onDestroyView() {
        hideDialog();
        mHandler.removeMessages(1000);
        super.onDestroyView();
        L.d("--fragment, onDestroyView -> " + getClass().getSimpleName());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        L.d("--fragment, onDestroy -> " + getClass().getSimpleName());
    }

    protected boolean isLogin() {
        String token = UserInfoManager.getInstance().getToken();
        if (StringUtil.isEmpty(token)) {
            T.showShort(mContext, "请登录!");
            startActivity(new Intent(mContext, RegistAndLoginActivity.class));
            return false;
        }
        return true;
    }

    protected void showProgressDialog() {
        showProgressDialog("加载中,请稍后...");
    }

    protected void showProgressDialog(String content) {
        showProgressDialog(content, true);
    }

    protected void showProgressDialog(String content, boolean cancelable) {
        mExitTime = System.currentTimeMillis();
        mWaitingDialog = new WaitingDialog();
        mWaitingDialog.setContent(content, cancelable);
        mWaitingDialog.show(getChildFragmentManager(), getClass().getSimpleName());
    }

    protected void hideDialog() {
        if (mWaitingDialog != null) {
            if ((System.currentTimeMillis() - mExitTime) > 500) {
                mWaitingDialog.dismissAllowingStateLoss();
            } else {
                mHandler.sendEmptyMessageDelayed(1000, 500 + mExitTime - System.currentTimeMillis());
            }
        }

        if (mCustomAlertDialog != null) {
            mCustomAlertDialog.dismiss();
        }
    }


    /**
     * 带取消和确认的对话框
     *
     * @param title
     * @param message
     * @param listener
     */
    protected void showCustomAlertDialog(String title, String message
            , CustomAlertDialog.CustomAlertDialogClickListener listener) {
        showCustomAlertDialog(title, message, "取消", "确认", listener);
    }

    /**
     * 对话框,不需要的参数传null
     *
     * @param title
     * @param message
     * @param negative
     * @param positive
     * @param listener
     */
    protected void showCustomAlertDialog(String title, String message, String negative, String positive
            , CustomAlertDialog.CustomAlertDialogClickListener listener) {
        mCustomAlertDialog = CustomAlertDialog.newInstance(
                new CustomAlertDialog.Builder(mContext)
                        .setTitle(title)
                        .setMessage(message)
                        .setNegative(negative)
                        .setPositive(positive)
                        .setListener(listener));
        mCustomAlertDialog.show(getChildFragmentManager());
    }


    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1000:
                    if (mWaitingDialog != null) {
                        mWaitingDialog.dismissAllowingStateLoss();
                    }
            }
        }
    };
}
